﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DenderaMvcWeb.Models;

namespace DenderaMvcWeb.Controllers
{
    public class OrdineController : Controller
    {
        private DenderaEntities db = new DenderaEntities();

        //
        // GET: /Ordine/

        public ActionResult Index()
        {
            var ordines = db.ordines.Include(o => o.anagrafica);
            return View(ordines.ToList());
        }

        //
        // GET: /Ordine/Details/5

        public ActionResult Details(int id = 0)
        {
            ordine ordine = db.ordines.Find(id);
            if (ordine == null)
            {
                return HttpNotFound();
            }
            return View(ordine);
        }

        //
        // GET: /Ordine/Create

        public ActionResult Create()
        {
            ViewBag.id_anag = new SelectList(db.anagraficas, "id_anag", "piva");
            return View();
        }

        //
        // POST: /Ordine/Create

        [HttpPost]
        public ActionResult Create(ordine ordine)
        {
            if (ModelState.IsValid)
            {
                db.ordines.Add(ordine);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.id_anag = new SelectList(db.anagraficas, "id_anag", "piva", ordine.id_anag);
            return View(ordine);
        }

        //
        // GET: /Ordine/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ordine ordine = db.ordines.Find(id);
            if (ordine == null)
            {
                return HttpNotFound();
            }
            ViewBag.id_anag = new SelectList(db.anagraficas, "id_anag", "piva", ordine.id_anag);
            return View(ordine);
        }

        //
        // POST: /Ordine/Edit/5

        [HttpPost]
        public ActionResult Edit(ordine ordine)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ordine).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_anag = new SelectList(db.anagraficas, "id_anag", "piva", ordine.id_anag);
            return View(ordine);
        }

        //
        // GET: /Ordine/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ordine ordine = db.ordines.Find(id);
            if (ordine == null)
            {
                return HttpNotFound();
            }
            return View(ordine);
        }

        //
        // POST: /Ordine/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ordine ordine = db.ordines.Find(id);
            db.ordines.Remove(ordine);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}